package remote;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.remoting.rmi.RmiServiceExporter;

import com.zsgl.util.ClassUtil;

public class ExportRemoteService implements ApplicationContextAware {
	
	AbstractApplicationContext context;
	
	List<String> exportPackage;
	
	public ExportRemoteService(List<String> exportPackage) {
		this.exportPackage = exportPackage;
	}
	
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.context = (AbstractApplicationContext)applicationContext;
	}

	public void exportRemoteService() {
		try {
			for (String packageName : exportPackage) {
				List<Class<?>> classes = ClassUtil.getAllInterface(packageName);
				for (Class<?> clazz : classes) {
					RmiServiceExporter obj = new RmiServiceExporter();
					obj.setServiceName(clazz.getSimpleName());
					obj.setServiceInterface(clazz);
					obj.setRegistryPort(8888);
					obj.setService(context.getBean(clazz));
					obj.prepare();
					System.out.println("Export Remote Interface：" + RemoteService.makeUrl("127.0.0.1", clazz));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

}
